package com.example.filemanagement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Connect {
    static public int LoginUser(String UserName,String PassWord) throws ClassNotFoundException, SQLException {
        if(UserName==""||PassWord==""){
            return -1;
        }
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库
        Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3307/file?useSSL=FALSE&serverTimezone=UTC"
                , "root", "123456");

        Statement DataStatement = DataConnection.createStatement();
        //查询用户名以否已经存在

        ResultSet UserIsExistResult = DataStatement.executeQuery("select * from student where user_id = '" + UserName + "'");
        if (UserIsExistResult.next()){
            String DBPassWord = UserIsExistResult.getString("password_user");
            if (DBPassWord.equals(PassWord)){
                //登录成功
                return 0;
            }
            else{
                return 2;
            }
        }
        else{
            return 1;
        }
    }
    static public int LoginManagement(String UserName,String PassWord) throws ClassNotFoundException, SQLException {
        if(UserName==""||PassWord==""){
            return -1;
        }
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库
        Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3307/file?useSSL=FALSE&serverTimezone=UTC"
                , "root", "123456");
        Statement DataStatement = DataConnection.createStatement();
        //查询用户名以否已经存在
        ResultSet UserIsExistResult = DataStatement.executeQuery("select * from admin where admin_id = '" + UserName + "'");
        if (UserIsExistResult.next()){
            String DBPassWord = UserIsExistResult.getString("password_admin");
            if (DBPassWord.equals(PassWord)){
                //登录成功
                return 0;
            }
            else{
                return 2;
            }
        }
        else{
            return 1;
        }

    }
    static public int RegisterUser(String Name,String UserName,String PassWord) throws SQLException, ClassNotFoundException{
        if(UserName==""||PassWord==""||Name==""){
            return -1;
        }
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库
        Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3307/file?useSSL=FALSE&serverTimezone=UTC"
                , "root", "123456");
        Statement DataStatement = DataConnection.createStatement();
        //查询用户名以否已经存在
        ResultSet UserIsExistResult = DataStatement.executeQuery("select user_id from student where user_id = '" + UserName + "'");
        if (UserIsExistResult.next()){
            //存在
            return 0;
        }
        else{
            //不存在,添加
            DataStatement.executeUpdate("INSERT INTO student (user_name,user_id,password_user)VALUES ('"+Name+"','" + UserName + "', '" + PassWord + "')");
            return 1;
        }
    }
    static public int RegisterManagement(String Name,String UserName,String PassWord) throws SQLException, ClassNotFoundException{
        if(UserName==""||PassWord==""||Name==""){
            return -1;
        }
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库
        Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3307/file?useSSL=FALSE&serverTimezone=UTC"
                , "root", "123456");
        Statement DataStatement = DataConnection.createStatement();
        //查询用户名以否已经存在
        ResultSet UserIsExistResult = DataStatement.executeQuery("select admin_name from admin where admin_name = '" + UserName + "'");
        if (UserIsExistResult.next()){
            //存在
            return 0;
        }
        else{
            //不存在,添加
            DataStatement.executeUpdate("INSERT INTO admin (admin_name,admin_id,password_admin)VALUES ('"+Name+"','" + UserName + "', '" + PassWord + "')");
            return 1;
        }
    }
    static public int Insert( String SendNumber,String SendMajor, String SendGender, String Sendage,
                              String SendGrade, String Sendbanji) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //连接数据库
        Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3307/file?useSSL=FALSE&serverTimezone=UTC",
                "root", "123456");
        Statement DataStatement = DataConnection.createStatement();
        ResultSet UserIsExistResult = DataStatement.executeQuery("select major from student where user_id = '" + SendNumber + "'");

            if (SendNumber == "" || SendMajor == "" || SendGender == "" || Sendage == "" || SendGrade == "" || Sendbanji == ""){
                return -1;
            }else{
                if (UserIsExistResult.next()){
                DataStatement.executeUpdate("update student set major='" + SendMajor + "',age=" + Sendage + ",gender='" + SendGender + "',grade=" + SendGrade + ",banji=" + Sendbanji + " where user_id='" + SendNumber + "'");

                DataStatement.executeUpdate("insert into dangan values ('在读','" + SendNumber + "',null,null,null)");
                return 1;
                }else {
                    DataStatement.executeUpdate("update student set major='" + SendMajor + "',age=" + Sendage + ",gender='" + SendGender + "',grade=" + SendGrade + ",banji=" + Sendbanji + " where user_id='" + SendNumber + "'");
                return 1;

                }
            }
    }
    static public boolean vd(String str){

        char[] chars=str.toCharArray();
        boolean isGB2312=false;
        for(int i=0;i<chars.length;i++){
            byte[] bytes=(""+chars[i]).getBytes();
            if(bytes.length==2){
                int[] ints=new int[2];
                ints[0]=bytes[0]& 0xff;
                ints[1]=bytes[1]& 0xff;
                if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){
                    isGB2312=true;
                    break;
                }
            }
        }
        return isGB2312;
    }
    public static boolean isNumeric(String str){

        for (int i = str.length();--i>=0;){

            if (!Character.isDigit(str.charAt(i))){

                return false;

            }

        }

        return true;

    }
}
